/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cgd;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Rodrigo
 */
public class HibernateUtil {
    
    private SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    private Session session = this.sessionFactory.openSession();
    
    private HibernateUtil() {
    }
    
    public static HibernateUtil getInstance() {
        return HibernateUtilHolder.INSTANCE;
    }
    
    private static class HibernateUtilHolder {

        private static final HibernateUtil INSTANCE = new HibernateUtil();
    }
    
     public SessionFactory getSessionFactory() {
        if(this.sessionFactory==null){
            this.sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
         return sessionFactory;
    }
    
    public Session getCurrentSession(){
        if(this.session==null){
            if(this.sessionFactory==null){
                this.sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
            }
            this.session = this.sessionFactory.openSession();
        }
        return session;
    }
}
